package com.etime.shycourse.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.dto.OrgTeacherDtoS;
import com.etime.shycourse.pojo.Organization;
import com.etime.shycourse.services.interfaces.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 机构基本信息
 *
 * @author wb
 * @since 2021-12-14
 */
@Api(tags = "后台-机构基本功能数据接口")
@RestController
@RequestMapping("/sys/org")
public class OrganizationController {
    @Autowired
    private OrganizationService service;

    @ApiOperation("根据机构负责人(用户id) 改为token 查询一条机构详细信息")
    @ApiImplicitParam("用户id 改为token 机构负责人对机构详细信息进行修改前的页面渲染")
    @GetMapping("/get")
    public PojoMessage<Organization> selectOrgByUserId(HttpServletRequest request) {
        String token = request.getHeader ("token");
        Organization data = service.selectOrgByUserId (token);
        PojoMessage<Organization> msg = new PojoMessage<> ();
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有指定的用户");
        }
        return msg;
    }

    @ApiOperation("根据机构负责人(用户id) 改为token 查询一条机构详细信息")
    @ApiImplicitParam("orgId  根据机构id修改机构详细信息")
    @GetMapping("/get/{orgId}")
    public PojoMessage<Organization> selectOrganizationById(@PathVariable Integer orgId) {
        PojoMessage<Organization> msg = new PojoMessage<> ();
        Organization data = service.selectById (orgId);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有指定的用户");
        }
        return msg;
    }

    @ApiOperation("机构负责人对机构详细信息进行修改 ")
    @ApiImplicitParam(name = "orgid", value = "必须传值 机构id 修改机构信息")
    @PostMapping("/update")
    public BaseMessage updateOrganization(Organization org) {
        BaseMessage msg = new BaseMessage ();
        int rtn = service.updateOrganization (org);
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("修改机构详细信息成功");
        } else {
            msg.setMsg ("修改机构详细信息失败");
        }
        return msg;
    }

    @ApiOperation("添加机构")
    @PostMapping("/inset")
    public BaseMessage insetOrganization(Organization org) {
        BaseMessage msg = new BaseMessage ();
        int rtn = service.insetOrganization (org);
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("创建机构成功");
        } else {
            msg.setMsg ("创建机构失败");
        }
        return msg;
    }

    @ApiOperation("查询所有的机构的信息")
    @GetMapping("/list")
    public ListMessage<Organization> selectTeacherListById() {
        ListMessage<Organization> msg = new ListMessage<> ();
        List<Organization> data = service.selectOrganizationAllList ();
        if (data != null) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到机构信息");
        }
        return msg;
    }

    @ApiOperation("机构分页查询列表  可使用机构名查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgname", value = "可选择条件 机构名"),
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少行"),
    })
    @GetMapping("/page")
    public PageMessage<OrgTeacherDtoS> selectOrganizationPage(Organization dto, Long pageNum, Long pageSize) {
        PageMessage<OrgTeacherDtoS> msg = new PageMessage<> ();
        Page<OrgTeacherDtoS> orgTeacherDtoPage = service.selectOrganizationPage (dto, pageNum, pageSize);
        if (orgTeacherDtoPage != null) {
            msg.setCode (1);
            msg.setPagedata (orgTeacherDtoPage);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到数据");
        }
        return msg;
    }
}
